﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace Finance.Core.Provider.XmlProvider
{
    public partial class XmlProvider : FinanceProvider
    {
        #region Abstract
        public override void Insert(CardItem item)
        {
            var list = CardItem.List;
            list.Add(item);

            this.WritePictureFile(list);
        }

        public override void Delete(CardItem item)
        {
            var list = CardItem.List;
            list.Remove(item);
            this.WritePictureFile(list);
        }

        public override void Update(CardItem item)
        {
            var list = CardItem.List;
            list.Remove(item);
            list.Add(item);
            this.WritePictureFile(list);
        }

        public override List<CardItem> GetAllCard()
        {
            var list = new List<CardItem>();
            var fileName = this.StorageFolder + typeof(CardItem).Name + ".xml";
            if (!File.Exists(fileName))
            {
                return list;
            }
            try
            {
                var doc = new XmlDocument();
                doc.Load(fileName);

                var nodes = doc.SelectNodes("Card/Item");
                if (nodes != null)
                {
                    nodes.Cast<XmlNode>().ToList().ForEach(node =>
                        {
                            CardItem item = new CardItem();

                            item.ID = node.Attributes["ID"] == null ? Guid.NewGuid().ToString() : node.Attributes["ID"].InnerText;
                            item.CardNo = node.Attributes["CardNo"] == null ? "" : node.Attributes["CardNo"].InnerText;
                            item.Amount = node.Attributes["Amount"] == null ? 0 : Double.Parse(node.Attributes["Amount"].InnerText);
                            item.Desc = node.Attributes["Desc"] == null ? null : node.Attributes["Desc"].InnerText;
                            GetBusinessBase<CardItem, String>(item, node);
                            list.Add(item);
                        });
                }
                list.Sort();
            }
            catch (Exception ex)
            {
                UtilLib.Util.Log(UtilLib.LogLevel.Error, ex);
            }
            return list;
        }

        public override CardItem GetCardItem(string id)
        {
            var item = CardItem.List.Find(p => p.ID == id) ?? new CardItem();
            item.MarkOld();
            return item;
        }

        public override void SaveAllCard()
        {
            this.WritePictureFile(CardItem.List);
        }
        #endregion

        private void WritePictureFile(List<CardItem> list)
        {
            var fileName = this.StorageFolder + typeof(CardItem).Name + ".xml";
            if (!System.IO.Directory.Exists(this.StorageFolder))
                System.IO.Directory.CreateDirectory(this.StorageFolder);
            if (System.IO.File.Exists(fileName))
            {
                FileStream stream = System.IO.File.Create(fileName);
                stream.Close();
                stream.Dispose();
            }
            using (var writer = new XmlTextWriter(fileName, Encoding.UTF8))
            {
                writer.Formatting = Formatting.Indented;
                writer.Indentation = 4;
                writer.WriteStartDocument(true);
                writer.WriteStartElement("Card");
                list.ForEach(item =>
                {
                    writer.WriteStartElement("Item");
                    writer.WriteAttributeString("ID", item.ID.ToString());
                    writer.WriteAttributeString("CardNo", item.CardNo);
                    writer.WriteAttributeString("Amount", item.Amount.ToString());
                    writer.WriteAttributeString("Desc", item.Desc);
                    WriteBusinessBase<CardItem, String>(item, writer);
                    writer.WriteEndElement();
                    item.MarkOld();
                });
                writer.WriteEndElement();
            }
        }
    }
}
